#include "mtk_tv.h"

MTK_TV::MTK_TV()
{
}

MTK_TV::~MTK_TV()
{
    Clear();
}
//success return channel number;fail return 0
//fp file pointer
//count need to read data numbers
//is_class :to decide atv-false(0) or dtv-true(1)
//
int MTK_TV::Read_BIN(FILE *fp,int start,int end,bool is_class)
{
    int i=0;
    if(!is_class)
      {
        while(1)
        {
            if(start+i*sizeof(MTK_ATV_DATA)>=end)
            {
                break;
            }
            fseek(fp,start+i*sizeof(MTK_ATV_DATA),SEEK_SET);
            MTK_ATV_DATA* mt_atv_data=(MTK_ATV_DATA*)malloc(sizeof(MTK_ATV_DATA));
            if(!mt_atv_data)
            {
                qDebug("In MTK_TV::Read_File malloc for mtk_atv_data failed!");
                return 0;
            }
            fread(mt_atv_data,1,sizeof(MTK_ATV_DATA),fp);
            mtk_atv.append(mt_atv_data);
            i++;
         }
    }else{
        while(1)
        {
            if(start+i*sizeof(MTK_DTV_DATA)>=end)
               break;
            fseek(fp,start+i*sizeof(MTK_DTV_DATA),SEEK_SET);
            MTK_DTV_DATA* mt_dtv_data=(MTK_DTV_DATA*)malloc(sizeof(MTK_DTV_DATA));
            if(!mt_dtv_data)
            {
                qDebug("In MTK_TV::Read_Fiel malloc for mtk_dtv_data failed");
                return 0;
            }
            fread(mt_dtv_data,1,sizeof(MTK_DTV_DATA),fp);
            mtk_dtv.append(mt_dtv_data);
            i++;
        }
    }
    return i;
}

int MTK_TV::Read_File(const char *disk)
{
    char file[FILE_PATH_LEN];
    memset(file,0,FILE_PATH_LEN);
    strcpy(file,disk);
    strcat(file,"mtk/tcl_clone_channellist.bin");
    file_name=file;
    FILE *fp=fopen(file,"rb");
    if(!fp)
    {
        qDebug("Open file tcl_clone_channelist.bin error");
        return -1;
    }
   // BIN_FILE_HEADER header;
    fread(&header,1,sizeof(BIN_FILE_HEADER),fp);
    //solution=header.flag>>4;
    int dtv_atv_flag=(header.flag&0x0f)>>2;
    //version=header.flag&0x03;
    int length=header.dtv_atv_length;
    int atv_start=0;
    int dtv_start=0;
    int end=0;
    fseek(fp,0,SEEK_END);
    end=ftell(fp);
    fseek(fp,0,SEEK_SET);
    switch(dtv_atv_flag)
    {
    case 0://just atv
        //count_atv=length;
        atv_start=sizeof(BIN_FILE_HEADER);
        count_atv=Read_BIN(fp,atv_start,SEEK_END,0);
        old_count_atv=count_atv;
        break;
    case 1:
        //count_dtv=length;
        dtv_start=sizeof(BIN_FILE_HEADER);
        count_dtv=Read_BIN(fp,dtv_start,SEEK_END,1);
        old_count_dtv=count_dtv;
        break;
    case 3:
        //count_dtv=length;
        dtv_start=sizeof(BIN_FILE_HEADER);
        atv_start=sizeof(BIN_FILE_HEADER)+length*sizeof(MTK_DTV_DATA);
        count_dtv=Read_BIN(fp,dtv_start,dtv_start,1);
        old_count_dtv=count_dtv;
        count_atv=Read_BIN(fp,dtv_start,end,0);
        old_count_atv=count_atv;
        break;
    case 2:
        //count_atv=length;
        atv_start=sizeof(BIN_FILE_HEADER);
        dtv_start=sizeof(BIN_FILE_HEADER)+sizeof(MTK_ATV_DATA)*length;
        count_atv=Read_BIN(fp,atv_start,dtv_start,0);
        old_count_atv=count_atv;
        count_dtv=Read_BIN(fp,dtv_start,end,1);
        old_count_dtv=count_dtv;
        break;
    default:
        qDebug("In MTK_TV::Read_File get the solution error\n");
        fclose(fp);
        return -1;
    }
   fclose(fp);
   Sort_ATV();
   Sort_DTV();
    return 1;


}

int MTK_TV::Write_BIN(FILE*fp,int num,bool is_class)
{
    if(!is_class)
    {
        char buf[FILE_LEN_MT_A];
        for(int i=0;i<num;i++)
        {
            memset(buf,0,FILE_LEN_MT_A);
            MTK_ATV_DATA*mt_atv_data=mtk_atv.at(i);
            memcpy(buf,mt_atv_data,FILE_LEN_MT_A);
            fwrite(buf,1,sizeof(MTK_ATV_DATA),fp);
            mt_atv_data=NULL;
        }
    }else{
        char pbuf[FILE_LEN_MT_D];
        for(int i=0;i<num;i++)
        {
            memset(pbuf,0,FILE_LEN_MT_D);
            MTK_DTV_DATA *mt_dtv_data=mtk_dtv.at(i);
            memcpy(pbuf,mt_dtv_data,FILE_LEN_MT_D);
            fwrite(pbuf,1,sizeof(MTK_DTV_DATA),fp);
            mt_dtv_data=NULL;
        }
    }
    return 1;
}
/*
*return value:
*0-open file error
*-1-bin file header error
*1-write success
*/

int MTK_TV::Write_File(const char *disk)
{
    char HEADER_MAGIC[8]={'C','H','A','N','N','E','L','\0'};
    char file[FILE_PATH_LEN];
    memset(file,0,FILE_PATH_LEN);
    strcpy(file,disk);
    strcat(file,"mtk/tmp_mtk_clone.bin");

    FILE *fp=fopen(file,"wb+");//追加方式
    if(!fp)
    {
        qDebug("Open file tcl_clone_channelist.bin error");
        return 0;
    }
    for(int i=0;i<8;i++)
        header.magic[i]=HEADER_MAGIC[i];

    //strncpy(header.magic,HEADER_MAGIC,8);
    header.dtv_atv_length=count_atv;
    char *buf=new char(sizeof(BIN_FILE_HEADER));
    memcpy(buf,&header,sizeof(BIN_FILE_HEADER));
    fwrite(buf,1,sizeof(BIN_FILE_HEADER),fp);
    int solution=header.flag>>2;
    switch(solution)
    {
      case 2:
        Write_BIN(fp,count_atv,0);
        Write_BIN(fp,count_dtv,1);
        break;
      case 3:
        Write_BIN(fp,count_dtv,1);
        Write_BIN(fp,count_atv,0);
        break;
      case 0:
        Write_BIN(fp,count_atv,0);
        break;
      case 1:
        Write_BIN(fp,count_dtv,1);
        break;
    default:
        qDebug("IN MTK_TV::Write_File get solution error");
        fclose(fp);
        return -1;
    }
    fclose(fp);
    return 1;
}

void MTK_TV::Del_ATV_Row(int row)
{
    //mtk_atv.at(row)->ui4_nw_flag&&251;//Delete 位置1，TV中不可见
    mtk_atv.removeAt(row);
    count_atv--;
}
void MTK_TV::Del_DTV_Row(int row)
{
   // mtk_dtv.at(row)->ui4_nw_flag&&251;
    mtk_dtv.removeAt(row);
    count_dtv--;
}

void MTK_TV::Mod_ATV(int row, int col, QString value)
{
    bool Flag=false;
    bool *ok=&Flag;
    switch(col)
    {
    case 0:
        mtk_atv.at(row)->ui4_ch_val=value.toUInt(ok);
        Q_ASSERT(*ok==true);
        break;
    case 1:
        memset(mtk_atv.at(row)->ps_name,0,MAX_SERVICE_NAME);
        strcpy(mtk_atv.at(row)->ps_name,value.toLatin1().data());
        break;
    case 2:
        if(0==value.compare("air"))
            mtk_atv.at(row)->ui4_nw_flag|=0;
        else
            mtk_atv.at(row)->ui4_nw_flag|=1<<3;
        //mtk_atv.at(row)->ui4_nw_flag|=(value.toUInt(ok)<<3);
      //  Q_ASSERT(*ok==true);
        break;
    case 3:
        mtk_atv.at(row)->ui4_freq=value.toUInt(ok);
        Q_ASSERT(*ok==true);
        break;
    case 4:
        mtk_atv.at(row)->ui4_nw_flag|=(value.toUInt(ok));
        Q_ASSERT(*ok==true);
        break;
     case 5:
        mtk_atv.at(row)->ui4_nw_flag|=(value.toUInt(ok)<<1);
        Q_ASSERT(*ok==true);
        break;
     default:
        qDebug("In MTK_TV::Mod_AV para col error");
    }
}

void MTK_TV::Mod_DTV(int row, int col, QString value)
{
     bool Flag=false;
     bool *ok=&Flag;
     switch(col)
     {
     case 0:
	  mtk_dtv.at(row)->ui4_ch_val=value.toUInt(ok);
	  Q_ASSERT(*ok==true);
	  break;
     case 1:
      memset(mtk_dtv.at(row)->ps_name,0,MAX_SERVICE_NAME);
      strcpy(mtk_dtv.at(row)->ps_name,value.toLatin1().data());
	  break;
     case 2:
      if(0==value.compare("DTV-T"))
             mtk_dtv.at(row)->ui4_nw_flag|=0;
      else if(0==value.compare("DTV-C"))
            mtk_dtv.at(row)->ui4_nw_flag|=1<<3;
      else if(0==value.compare("DTV-S/S2"))
          mtk_dtv.at(row)->ui4_nw_flag=(2<<3);
      else
          mtk_dtv.at(row)->ui4_nw_flag=(3<<3);
	  break;

     case 3:
         uint tv_type;
         if(0==value.compare("D"))
             tv_type=1;
         else if(0==value.compare("R"))
             tv_type=2;
         else if(0==value.compare("T"))
             tv_type=3;
         else if(0==value.compare("U"))
             tv_type=4;
         else
             tv_type=5;
      mtk_dtv.at(row)->ui2_type=tv_type;
      //Q_ASSERT(*ok==true);
	  break;
     case 4:
      mtk_dtv.at(row)->ui4_freq=value.toUInt(ok);
	  Q_ASSERT(*ok==true);
	  break;
     case 5:
	  mtk_dtv.at(row)->ui4_nw_flag|=(value.toUInt(ok));
	  Q_ASSERT(*ok==true);
	  break;
     case 6:
	  mtk_dtv.at(row)->ui4_nw_flag|=(value.toUInt(ok)<<1);
	  Q_ASSERT(*ok==true);
	  break;
     default:
	  qDebug("In MTK_TV::Mod_DTV para col error");
     }
}


uint MTK_TV::Get_ATV_Num(int row)
{
    return mtk_atv.at(row)->ui4_ch_val;
}

uint MTK_TV::Get_DTV_Num(int row)
{
    return mtk_dtv.at(row)->ui4_ch_val;
}

void MTK_TV::Get_Chan_Num(QList<uint> *atv_Num, QMap<uint, uchar> *dtv_air_Num)

{
    for(int i=0;i<mtk_atv.size();i++)
    {
       // if(0==(mtk_atv.at(i)->ui4_nw_flag&&4))
       // {
            atv_Num->append(mtk_atv.at(i)->ui4_ch_val);
       // }
    }
    for(int i=0;i<mtk_dtv.size();i++)
    {
       // if(0==(mtk_dtv.at(i)->ui4_nw_flag&&4))
        //{
           // if(0==(mtk_dtv.at(i)->ui4_nw_flag&&8))
           // {
            //MTK all channel numbers different
        dtv_air_Num->insert(mtk_dtv.at(i)->ui4_ch_val,mtk_dtv.at(i)->ui2_type);
           // }else if(1==(mtk_dtv.at(i)->ui4_nw_flag&&8))
           // {
           //     dtv_cable_Num->insert(mtk_dtv.at(i)->ui4_ch_val,mtk_dtv.at(i)->ui2_type);
            //}else{

           // }
       // }
    }
}

bool Sort_By_Type(MTK_DTV_DATA *a,MTK_DTV_DATA *b)
{
    return a->ui2_type<b->ui2_type;
}


bool Sort_ATV_By_Num(MTK_ATV_DATA *a,MTK_ATV_DATA *b)
{
    return a->ui4_ch_val<b->ui4_ch_val;
}

bool Sort_DTV_By_Num(MTK_DTV_DATA *a,MTK_DTV_DATA *b)
{
    return a->ui4_ch_val<b->ui4_ch_val;
}

void MTK_TV::Add_DTV_CH(QList<QString> Item)
{
    count_dtv++;
    old_count_dtv++;
    MTK_DTV_DATA *mtk_dtv_data=(MTK_DTV_DATA*)malloc(sizeof(MTK_DTV_DATA));
    memset(mtk_dtv_data,0,sizeof(MTK_DTV_DATA));
    mtk_dtv.append(mtk_dtv_data);
    for(int i=0;i<NUM_DTV_ITEM;i++)
    {
        Mod_DTV(count_dtv-1,i,Item.at(i));
    }
}

void MTK_TV::Add_ATV_CH(QList<QString> Item)
{
    count_atv++;
    old_count_atv++;
    MTK_ATV_DATA *mtk_atv_data=(MTK_ATV_DATA*)malloc(sizeof(MTK_ATV_DATA));
    memset(mtk_atv_data,0,sizeof(MTK_ATV_DATA));
    mtk_atv.append(mtk_atv_data);
    for(int i=0;i<NUM_TV_ITEM;i++)
    {
        Mod_ATV(count_atv-1,i,Item.at(i));
    }

}

uint MTK_TV::Get_ATV_Count()
{
    return count_atv;
}

uint MTK_TV::Get_DTV_Count()
{
    return count_dtv;
}

bool MTK_TV::Is_Empty()
{
    if(0==empty)
    {
        return true;
    }else
        return false;
}

void MTK_TV::Empty_Add_One()
{
    empty++;
}
void MTK_TV::Empty_Dul_One()
{
    empty--;
}

void MTK_TV::set_Empty(uint num)
{
    empty=num;
}

void MTK_TV::Clear()
{
    for(int i=0;i<mtk_dtv.size();i++)
    {
        free(mtk_dtv.at(i));
    }
    mtk_dtv.clear();
    for(int j=0;j<mtk_atv.size();j++)
    {
        free(mtk_atv.at(j));
    }
    mtk_atv.clear();
}


void MTK_TV::Clear_Data()
{
    mtk_dtv.clear();
    mtk_atv.clear();
}


void MTK_TV::Sort_ATV()
{
    qSort(mtk_atv.begin(),mtk_atv.end(),Sort_ATV_By_Num);
}

void MTK_TV::Sort_DTV()
{
    qSort(mtk_dtv.begin(),mtk_dtv.end(),Sort_By_Type);
    qSort(mtk_dtv.begin(),mtk_dtv.end(),Sort_DTV_By_Num);
}
